Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
Angular ngStyle Parser Error: Unexpected token [, expected identifier or keyword

I need to be able to pass a dynamic string using the fieldName in order to get an attribute within the item object. In order to make it easy to duplicate the issue I have given a small example. The problem is this syntax works properly with angular 12.2.5 as I checked but did not work with angular 12.0.0 or below. I can simply call a function but I know this is not good for performance. Is there an alternate way to write this syntax for older angular applications?

HTML

<p [style.background]="item?.[fieldName]">
  Some Text
</p>

ts

item =  { warn: 'yellow', success: 'green', error: 'red' };
fieldName = 'success';

Error looks like this

Parser Error: Unexpected token [, expected identifier or keyword at column 20 in [{background: item?.[fieldName]}]
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try to use ternary operator ?::

<p [style.background]="item[fieldName] ? item[fieldName] : ''">
  Some Text
</p>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda